package br.ufrgs.meetapp.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import br.ufrgs.meetapp.activities.LoginActivity;
import br.ufrgs.meetapp.activities.R;

public class PopupLogin {
	
	private AlertDialog alert;
	private Context context;
	
	private String title;
	private String message;
	private String firstButtonName;
	private String secondButtonName;
	private String usernameTextView;
	private String passwordTextView;
	private TextView dialogUsernameTextView;
	private TextView dialogPasswordTextView;
	
	/***
	* Creates a popup with the values passed <br>
	* Your activity should implement DialogInterface.OnClickListener in order to handle the button clicks <br><br>
	* <b>Example:</b> <br>
	* 
	*<b>public void onClick(DialogInterface dialog, int which)</b> { <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;	Toast toast = Toast.makeText(this, "nenhum dos dois", Toast.LENGTH_LONG); <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;	if(which == dialog.BUTTON1) <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;		toast = Toast.makeText(this, "opcoes", Toast.LENGTH_LONG); <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;	else if(which == dialog.BUTTON2) <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;		toast = Toast.makeText(this, "cancelar", Toast.LENGTH_LONG); <br>
	*&nbsp;&nbsp;&nbsp;&nbsp; toast.show(); <br>
	*}
	* 
	* @param context - activity context
	* @param title - popup title
	* @param message - message that will be displayed
	* @param firstButtonName - lable of the first button
	* @param firstButtonListener - listener that handles clicks on the first button
	*/
	public PopupLogin(Context context, String title, String message, String firstButtonName, DialogInterface.OnClickListener firstButtonListener) {
		this(context, title, message, firstButtonName, null, firstButtonListener, null);
	}

	/***
	* Creates a popup with the values passed <br>
	* Your activity should implement DialogInterface.OnClickListener in order to handle the button clicks <br><br>
	* <b>Example:</b> <br>
	* 
	*<b>public void onClick(DialogInterface dialog, int which)</b> { <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;	Toast toast = Toast.makeText(this, "nenhum dos dois", Toast.LENGTH_LONG); <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;	if(which == dialog.BUTTON1) <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;		toast = Toast.makeText(this, "opcoes", Toast.LENGTH_LONG); <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;	else if(which == dialog.BUTTON2) <br>
	*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;		toast = Toast.makeText(this, "cancelar", Toast.LENGTH_LONG); <br>
	*&nbsp;&nbsp;&nbsp;&nbsp; toast.show(); <br>
	*}
	* 
	* @param context - activity context
	* @param title - popup title
	* @param message - message that will be displayed
	* @param firstButtonName - lable of the first button
	* @param secondButtonName - lable of the second button
	* @param firstButtonListener - listener that handles clicks on the first button
	* @param secondButtonListener - listener that handles clicks on the second button
	*/
	public PopupLogin(Context context, String title, String message, String firstButtonName, String secondButtonName, DialogInterface.OnClickListener firstButtonListener, DialogInterface.OnClickListener secondButtonListener) {
		
		this.context = context;
		this.title = title;
		this.message = message;
		this.firstButtonName = firstButtonName;
		this.secondButtonName = secondButtonName;
		
		try {
			LayoutInflater factory = LayoutInflater.from(context);
			final View textEntryView = factory.inflate(R.layout.request_password_dialog, null);
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(title);
			builder.setCancelable(false);
			builder.setMessage(message);
			builder.setView(textEntryView);
			
			dialogUsernameTextView = (TextView) textEntryView.findViewById(R.id.username_edit);
			dialogUsernameTextView.setText(LoginActivity.loggedUser.getUsername());
			
			dialogPasswordTextView = (TextView) textEntryView.findViewById(R.id.password_edit);
			dialogPasswordTextView.requestFocus();
			
			if((firstButtonName != null) && (!firstButtonName.equalsIgnoreCase("")))
				builder.setPositiveButton(firstButtonName, firstButtonListener);
			
			if((secondButtonName != null) && (!secondButtonName.equalsIgnoreCase(""))) {
				builder.setNegativeButton(secondButtonName, secondButtonListener);
			}
			
			alert = builder.create();
			
		} catch(Exception exception) {
			System.out.println("Error in PopupMessage.PopupMessage - trying to create new dialog - ");
			if(exception.getMessage() != null) 
				System.out.println(exception.getMessage());
			exception.printStackTrace();
		}
		
	}

	/**
	 * Shows the popup in screen
	 * @return true if went ok
	 */
	public boolean display() {
		
		boolean displayed = false;
		//show Dialog
		try {
			alert.show();			
		} catch(Exception exception) {
			System.out.println("Error in PopupMessage.display - trying to display the message - ");
			if(exception.getMessage() != null)
				System.out.println(exception.getMessage());
			exception.printStackTrace();
		}
		
		return displayed;
	}
	
	/**
	 * Hides the popup - i dont think this should be used
	 */
	public void dismiss() {
		if(alert != null)
			alert.dismiss();
	}

	public AlertDialog getAlert() {
		return alert;
	}

	public void setAlert(AlertDialog alert) {
		this.alert = alert;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getFirstButtonName() {
		return firstButtonName;
	}

	public void setFirstButtonName(String firstButtonName) {
		this.firstButtonName = firstButtonName;
	}

	public String getSecondButtonName() {
		return secondButtonName;
	}

	public void setSecondButtonName(String secondButtonName) {
		this.secondButtonName = secondButtonName;
	}

	public String getUsernameTextView() {
		return usernameTextView;
	}

	public void setUsernameTextView(String usernameTextView) {
		this.usernameTextView = usernameTextView;
	}

	public String getPasswordTextView() {
		return passwordTextView;
	}

	public void setPasswordTextView(String passwordTextView) {
		this.passwordTextView = passwordTextView;
	}

	public TextView getDialogUsernameTextView() {
		return dialogUsernameTextView;
	}

	public void setDialogUsernameTextView(TextView dialogUsernameTextView) {
		this.dialogUsernameTextView = dialogUsernameTextView;
	}

	public TextView getDialogPasswordTextView() {
		return dialogPasswordTextView;
	}

	public void setDialogPasswordTextView(TextView dialogPasswordTextView) {
		this.dialogPasswordTextView = dialogPasswordTextView;
	}
}
